﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using r4u.data.access.layer.REGISTER;

namespace r4u.data.access.layer.RECRUIT
{
    public class VacancyDAO
    {
        private UserDAO user = new UserDAO();

        public bool Update(VACANCY vacancy)
        {
            using (var context = new r4uEntities())
            {
                VACANCY v = context.VACANCies.FirstOrDefault(x => x.vacancy_id.Equals(vacancy.vacancy_id));
                v.vacancy_id = vacancy.vacancy_id;
                v.cust_id = vacancy.cust_id;
                v.vacancy_for = vacancy.vacancy_for;
                v.industry = vacancy.industry;
                v.description = vacancy.description;
                v.valid_from = vacancy.valid_from;
                v.valid_to = vacancy.valid_to;
                v.location_of_applicant = vacancy.location_of_applicant;
                v.education_level = vacancy.education_level;
                v.experience_from = vacancy.experience_from;
                v.experience_to = vacancy.experience_to;
                v.age_from = vacancy.age_from;
                v.age_to = vacancy.age_to;
                v.max_applications = vacancy.max_applications;
                v.allow_non_eligible = vacancy.allow_non_eligible;
                v.status = vacancy.status;

                if (!v.status.Equals("EXPIRED"))
                {
                    context.SaveChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }


        public bool DeleteVacancy(string vacancy_id)
        {
            using (var context = new r4uEntities())
            {
                VACANCY v = context.VACANCies.FirstOrDefault(x => x.vacancy_id.Equals(vacancy_id));

                if (v.status.Equals("PLANNED"))
                {
                    context.VACANCies.Remove(v);
                    context.SaveChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }


        public VACANCY Copy(string email, string vacancy_id)
        {
            using (var context = new r4uEntities())
            {
                VACANCY v = context.VACANCies.FirstOrDefault(x => x.vacancy_id.Equals(vacancy_id));

                VACANCY vacancy = new VACANCY();
                vacancy.vacancy_id = user.GenerateNewCode(7);
                vacancy.cust_id = user.GetCustIdByEmail(email);
                vacancy.status = "PLANNED";
                vacancy.vacancy_for = v.vacancy_for;
                vacancy.industry = v.industry;
                vacancy.description = v.description;
                vacancy.valid_from = v.valid_from;
                vacancy.valid_to = v.valid_to;
                vacancy.location_of_applicant = v.location_of_applicant;
                vacancy.education_level = v.education_level;
                vacancy.experience_from = v.experience_from;
                vacancy.experience_to = v.experience_to;
                vacancy.age_from = v.age_from;
                vacancy.age_to = v.age_to;
                vacancy.max_applications = v.max_applications;
                vacancy.allow_non_eligible = v.allow_non_eligible;
                context.VACANCies.Add(vacancy);
                context.SaveChanges();
                return vacancy;
            }
        }

        public bool AddVacancy(string email,VACANCY vacancy)
        {
            try
            {
                vacancy.vacancy_id = user.GenerateNewCode(7);
                vacancy.cust_id = user.GetCustIdByEmail(email);
                vacancy.status = "PLANNED";

                using (var context = new r4uEntities())
                {
                    context.VACANCies.Add(vacancy);
                    context.SaveChanges();
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }


        public VACANCY[] GetVacancies(string email)
        {
            CUSTOMER_ACCOUNT account = user.GetUser(email);

            return account.VACANCies.Cast<VACANCY>().ToList().ToArray();
        }


        public VACANCY GetVacancy(string vacancy_id)
        {
            using (var context = new r4uEntities())
            {
                return context.VACANCies.FirstOrDefault(x => x.vacancy_id.Equals(vacancy_id));
            }
        }


        public VACANCY GetVacancyWithSkills(string vacancy_id)
        {
            using (var context = new r4uEntities())
            {
                return context.VACANCies.Include("VACANCY_SKILL").Include("CUSTOMER_ACCOUNT").FirstOrDefault(x => x.vacancy_id.Equals(vacancy_id));
            }
        }


        public bool InsertApplicant(string vacancy_id, string applicant_id, XmlDocument profile)
        {
            try
            {
                VACANCY_APPLICANT applicant = new VACANCY_APPLICANT();
                applicant.vacancy_id = vacancy_id;
                applicant.applicant_id = applicant_id;
                applicant.profile_data = profile.OuterXml;

                using (var context = new r4uEntities())
                {
                    context.VACANCY_APPLICANT.Add(applicant);
                    context.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public List<VACANCY_APPLICANT> GetApplicants(string vacancy_id)
        {
            using (var context = new r4uEntities())
            {
                VACANCY v = context.VACANCies.Include("VACANCY_APPLICANT").FirstOrDefault(x => x.vacancy_id.Equals(vacancy_id));
                return v.VACANCY_APPLICANT.ToList();
            }
        }

        public static string ToString(object source, Type type, Encoding encoding)
        {
            // The string to hold the object content
            String content;

            // Create a memoryStream into which the data can be written and readed
            using (var stream = new MemoryStream())
            {
                // Create the xml serializer, the serializer needs to know the type
                // of the object that will be serialized
                var xmlSerializer = new XmlSerializer(type);

                // Create a XmlTextWriter to write the xml object source, we are going
                // to define the encoding in the constructor
                using (var writer = new XmlTextWriter(stream, encoding))
                {
                    // Save the state of the object into the stream
                    xmlSerializer.Serialize(writer, source);

                    // Flush the stream
                    writer.Flush();

                    // Read the stream into a string
                    using (var reader = new StreamReader(stream, encoding))
                    {
                        // Set the stream position to the begin
                        stream.Position = 0;

                        // Read the stream into a string
                        content = reader.ReadToEnd();
                    }
                }
            }

            // Return the xml string with the object content
            return content;
        }
    }
}
